package com.cqtczh.qhd.driver.acticity.carteam;

import httptools.HttpJsonTool;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javabeen.OrderListBeen;
import javabeen.Row;

import org.json.JSONException;
import org.json.JSONObject;

import tools.L;

import adapter.OrderListAdapter;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.cqtczh.qhd.driver.AppContent;
import com.cqtczh.qhd.driver.R;
import com.cqtczh.qhd.driver.acticity.base.BaseActivity;
import com.cqtczh.qhd.driver.acticity.order.OrderDetails;
import com.cqtczh.qhd.driver.acticity.order.OrdersActivity;
import com.cqtczh.qhd.driver.acticity.user.UserLoginActivity;
import com.cqtczh.qhd.driver.api.GagApi;
import com.cqtczh.qhd.driver.model.CargoInfo;
import com.cqtczh.qhd.driver.utils.StringUtils;
import com.cqtczh.qhd.driver.vo.JsonRequest;
import com.cqtczh.qhd.driver.vo.JsonResponse;
import com.cqtczh.qhd.driver.vo.State;

public class CarTeamMemberOrders extends BaseActivity {
	private ListView listView;
	private List<Map<String, Object>> get_data = new ArrayList<Map<String, Object>>();
	private String driverPhone;
	private int CarTeamId;
	private List<OrderListBeen> sourceDateList;
	private TextView member_uncomplete;
	private TextView member_completeing;
	private TextView member_member_completed;
	private String flowStatus = "1";
	private OrderListAdapter adapter;
	private boolean flag = false;
	private int no = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_car_team_member_orders);
		Ini();
	}

	private void Ini() {
		sourceDateList=new ArrayList<OrderListBeen>();
		listView = (ListView) findViewById(R.id.listView1);
		driverPhone = getIntent().getStringExtra("driverPhone");
		member_uncomplete = (TextView) findViewById(R.id.member_uncomplete);
		member_uncomplete.setBackgroundColor(Color.parseColor("#FFD700"));
		member_completeing = (TextView) findViewById(R.id.member_completeing);
		member_completeing.setBackgroundColor(Color.parseColor("#F5F5DC"));
		member_member_completed = (TextView) findViewById(R.id.member_completed);
		member_completeing.setBackgroundColor(Color.parseColor("#F5F5DC"));
		member_uncomplete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (no == 0) {

				}
				if (no == 1) {
					no = 0;
					member_completeing.setBackgroundColor(Color
							.parseColor("#F5F5DC"));
				}
				if (no == 2) {
					no = 0;
					member_member_completed.setBackgroundColor(Color
							.parseColor("#F5F5DC"));
				}
				member_uncomplete.setBackgroundColor(Color
						.parseColor("#FFD700"));
				get_data.clear();
				flowStatus = "1";
				flag = true;
				GetOrdersData();
			}
		});
		member_completeing.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (no == 0) {
					no = 1;
					member_uncomplete.setBackgroundColor(Color
							.parseColor("#F5F5DC"));
				}
				if (no == 1) {

				}
				if (no == 2) {
					no = 1;
					member_member_completed.setBackgroundColor(Color
							.parseColor("#F5F5DC"));
				}
				member_completeing.setBackgroundColor(Color
						.parseColor("#FFD700"));
				get_data.clear();
				flowStatus = "2";
				flag = true;
				GetOrdersData();
			}
		});
		member_member_completed.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (no == 0) {
					no = 2;
					member_uncomplete.setBackgroundColor(Color
							.parseColor("#F5F5DC"));
				}
				if (no == 1) {
					no = 2;
					member_completeing.setBackgroundColor(Color
							.parseColor("#F5F5DC"));
				}
				if (no == 2) {

				}
				member_member_completed.setBackgroundColor(Color
						.parseColor("#FFD700"));
				get_data.clear();
				flowStatus = "3";
				flag = true;
				GetOrdersData();
			}
		});
		if (driverPhone != null && (!driverPhone.equals(""))) {
			GetOrdersData();
		}
		Button callphone = (Button) findViewById(R.id.callphone);
		callphone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				Intent intent = new Intent();
				// System.out.println("real电话");
				// 激活源代码,添加intent对象
				intent.setAction("android.intent.action.CALL");
				// intent.addCategory("android.intent.category.DEFAULT");内部会自动添加类别，
				intent.setData(Uri.parse("tel:" + driverPhone));
				// 激活Intent
				startActivity(intent);
			}
		});

	}

	private void GetOrdersData() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("state", flowStatus);
		dataMap.put("carownerId", driverPhone);
		showLoadingDialog("加载中...");
		// 如果是有数据变化那么这里应该变为false
		HttpJsonTool httpJsonTool = new HttpJsonTool(this);
		httpJsonTool.SetOnLister(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				dismissLoadingDialog();
				if (msg.what == 1) {
					List<OrderListBeen> data = (List<OrderListBeen>) msg.obj;
					sourceDateList.clear();
					for (int i = 0; i < data.size(); i++) {
						OrderListBeen ds=data.get(i);
						sourceDateList.add(ds);
					}
					ListConter();
				} else {
					Toast.makeText(CarTeamMemberOrders.this, (String) msg.obj,
							Toast.LENGTH_LONG).show();
				}
			}
		});

		httpJsonTool.SetRowOnLister(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 1) {
					Row row = (Row) msg.obj;
					Toast.makeText(
							CarTeamMemberOrders.this,
							"获得的订单总数：" + (Integer.parseInt(row.getTotal()) + 1)
									+ "", Toast.LENGTH_LONG).show();
				}
			}
		});

		Type type = new TypeToken<List<OrderListBeen>>() {
		}.getType();

		httpJsonTool.StartWork(dataMap, GagApi.GETPLANLIST, type, null);
	}

	private void ListConter() {

		if (flag) {
			//GetOrdersData();
			adapter.notifyDataSetChanged();
		} else {
			setDataToListView();
		}

	}

	
	
	
	private void setDataToListView() {
		// TODO Auto-generated method stub
		listView=(ListView) findViewById(R.layout.orderlistview);
		adapter=new OrderListAdapter(this,sourceDateList , R.layout.activity_myorder_conent);
		listView.setAdapter(adapter);
	}

	

}
